package date0702pm;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import cn.edu.ecut.util.DatabaseType;
import cn.edu.ecut.util.JdbcHelper;

public class JdbcTest10 {

	public static void main(String[] args) throws SQLException {
		
		List<Student> list = new ArrayList<>();
		
		JdbcHelper h = JdbcHelper.getInstance( DatabaseType.ORACLE );
		Statement s = h.create() ;
		
		String select = "SELECT id , name , gender , birthdate FROM t_students ORDER BY id DESC" ;
		ResultSet rs = s.executeQuery( select );
		
		while( rs.next() ) { // 如果 select 语句返回了数据则结果集就会有数据，则说明
			Student t = new Student(); 
			
			int id = rs.getInt( "id" ); // rs.getInt( 1 );
			t.setId(id);
			
			String name = rs.getString( "name" ) ; // rs.getString( 2 );
			t.setName(name);
			
			String genderString = rs.getString( "gender" ); // rs.getString( 3 );
			if( genderString != null && ( genderString = genderString.trim() ).length() > 0  ) {
				char gender = genderString.charAt( 0 ); // 取字符串中首个字符
				t.setGender(gender);
			}
			
			java.sql.Date date = rs.getDate( "birthdate" );  // rs.getDate( 4 )
			if( date != null ) {
				java.time.LocalDate birthdate = date.toLocalDate();
				t.setBirthdate( birthdate );
			}
			// 将封装好的 Student 对象添加到集合中
			list.add( t );
		}
		
		h.close( rs );
		h.close( s );
		h.release();
		
		for( Student t : list ) {
			System.out.println( t.getId() + " ,  " + t.getName() + " , " + t.getGender() + " , " + t.getBirthdate() );
		}

	}

}
